#!/usr/bin/wish -f

# frontend for the photometry script xebm.pl
# miroslav.broz@usa.net, http://www.astrohk.cz, Feb 20th 1999

# default values

set name "V541Cas"
set files "541cas_*"
set date "990110"
set cmp "001"
set df "dark.fits"
set ff "flat.fits"
set ap 3
set var 0
set konve 1
set darkbat 0
set flatbat 1
set muniphot 1
set munimatch 1
set munilist 1
set xebm 1

# try to read default values from xebm.config file

set in [open "./xebm.config" r]
gets $in name
# skip comments
while { [regexp "^#.*" $name] } {
  gets $in name
}
gets $in files
gets $in date
gets $in cmp
gets $in df
gets $in ff
gets $in ap
gets $in var
gets $in konve
gets $in darkbat
gets $in flatbat
gets $in muniphot
gets $in munimatch
gets $in munilist
gets $in xebm
close $in

# teporal varibles

set xsize 52
set ysize 39

frame .f01
frame .f02
frame .f03
frame .f04
frame .f05
frame .f06
frame .f1
frame .f2
frame .f3
frame .f4
frame .f5
frame .f6
frame .f7
frame .f8
frame .f9
frame .f10
frame .f11
frame .f12

label .l01
label .l03
label .l04
label .l05
label .l1 -text "name" -width 5
label .l2 -text "files" -width 5
label .l3 -text "date" -width 5

label .l4  -text "konve"     -width 9
label .l5  -text "darkbat"   -width 9
label .l6  -text "flatbat"   -width 9
label .l7  -text "muniphot"  -width 9
label .l8  -text "munimatch" -width 9
label .l9  -text "munilist"  -width 9
label .l10 -text "xebm"      -width 9
label .l11 -text "cmp"

entry .e1 -width 10 -textvariable name
entry .e3 -width 10 -textvariable date
entry .e2 -width 10 -textvariable files

button .b3 -text "View" -width 4

entry .e4 -width 10 -textvariable df
entry .e5 -width 10 -textvariable ff
entry .e6 -width 3 -textvariable cmp

checkbutton .c4 -variable konve
checkbutton .c5 -variable darkbat
checkbutton .c6 -variable flatbat
checkbutton .c7 -variable muniphot
checkbutton .c8 -variable munimatch
checkbutton .c9 -variable munilist
checkbutton .c10 -variable xebm

button .b1 -text "Run"
button .b2 -text "Quit"
button .b4 -text "Help"

menubutton .ap -text "aperture" -menu .ap.menu -relief raised -width 7
menu .ap.menu -tearoff 0
.ap.menu add radiobutton -label "A1 (2.0)"     -variable ap -value "1"
.ap.menu add radiobutton -label "A2 (2.7273)"  -variable ap -value "2"
.ap.menu add radiobutton -label "A3 (3.8182)"  -variable ap -value "3"
.ap.menu add radiobutton -label "A4 (5.2727)"  -variable ap -value "4"
.ap.menu add radiobutton -label "A5 (7.0909)"  -variable ap -value "5"
.ap.menu add radiobutton -label "A6 (9.2727)"  -variable ap -value "6"
.ap.menu add radiobutton -label "A7 (11.8182)" -variable ap -value "7"
.ap.menu add radiobutton -label "A8 (14.7273)" -variable ap -value "8"
.ap.menu add radiobutton -label "A9 (18.0)"    -variable ap -value "9"
.ap.menu add radiobutton -label "AA (21.6364)" -variable ap -value "10"
.ap.menu add radiobutton -label "AB (25.6364)" -variable ap -value "11"
.ap.menu add radiobutton -label "AC (30.0)"    -variable ap -value "12"
.ap.menu add radiobutton -label "IS (20.0)"    -variable ap -value "13"
.ap.menu add radiobutton -label "OS (30.0)"    -variable ap -value "14"
.ap.menu invoke $ap

menubutton .var -text "variable" -menu .var.menu -relief raised -width 7
menu .var.menu -tearoff 0
.var.menu add radiobutton -label "apview" -variable var -value "0"
.var.menu add radiobutton -label "star 1" -variable var -value "1"
.var.menu add radiobutton -label "star 2" -variable var -value "2"
.var.menu add radiobutton -label "star 3" -variable var -value "3"
.var.menu add radiobutton -label "star 4" -variable var -value "4"
.var.menu add radiobutton -label "star 5" -variable var -value "5"
.var.menu add radiobutton -label "star 6" -variable var -value "6"
.var.menu add radiobutton -label "star 7" -variable var -value "7"
.var.menu add radiobutton -label "star 8" -variable var -value "8"
.var.menu add radiobutton -label "star 9" -variable var -value "9"
.var.menu add radiobutton -label "star 10" -variable var -value "10"
.var.menu invoke $var

canvas .ci1 -width $xsize -height $ysize

if { [catch {image create photo .i1 -file "xebm_logo.gif" -width $xsize -height $ysize}] } then {
  .ci1 create text [expr $xsize / 2] [expr $ysize / 2] -text "XEBM"
} else {
  .ci1 create image [expr $xsize / 2] [expr $ysize / 2] -image .i1
}

pack .f03 -pady 3
pack .f01 -in .f03 -side left
pack .f1 -in .f01 -pady 3
pack .l1 .e1 -in .f1 -side left
pack .f3 -in .f01
pack .l3 .e3 -in .f3 -side left
pack .f2 -in .f01 -pady 3
pack .l2 .e2 -in .f2 -side left

pack .f02 -in .f03 -side left
pack .ci1 -in .f02 -pady 4
pack .b3 -in .f02 -padx 5

pack .f05
pack .f04 -in .f05 -side left
pack .f4 -in .f04
pack .c4 .l4 -in .f4 -side left
pack .f5 -in .f04
pack .c5 .l5 -in .f5 -side left
pack .f6 -in .f04 -pady 2
pack .c6 .l6 -in .f6 -side left
pack .f7 -in .f04
pack .c7 .l7 -in .f7 -side left
pack .f8 -in .f04
pack .c8 .l8 -in .f8 -side left
pack .f9 -in .f04 -pady 3
pack .c9 .l9 -in .f9 -side left
pack .f10 -in .f04
pack .c10 .l10 -in .f10 -side left

pack .f06 -in .f05
pack .l04 -in .f05
pack .e4 -in .f05 -pady 2
pack .e5 -in .f05
pack .l05 -in .f05
pack .f12 -in .f05
pack .l11 .e6 -in .f12 -side left
pack .ap -in .f05 -pady 3
pack .var -in .f05

pack .f11 -pady 5
pack .b1 -in .f11 -side left -padx 5
pack .b2 -in .f11 -side left
pack .b4 -in .f11 -padx 5

bind .b1 <ButtonPress-1> {
  write_config
  exec xterm -e ./xebm.pl
}
bind .b2 <ButtonPress-1> {
  write_config
  exit
}
bind .b3 <ButtonPress-1> {
  exec xterm -e ccd $files &
}
bind .b4 <ButtonPress-1> {
  destroy .help
  toplevel .help
  button .help.b1 -text "Okay" -command { destroy .help }
  message .help.l1 -justify center -width 300 -text "xebm.tk help\n\nmiroslav.broz@usa.net\nhttp://www.astrohk.cz\nFeb 9th 1999"
#  scrollbar .help.s1 -orient vertical
  pack .help.l1
  pack .help.b1 -pady 5
}

########################################################################
proc write_config {} {
  global name files date cmp df ff ap var konve darkbat flatbat\
    muniphot munimatch munilist xebm

  set id [ open "./xebm.config" w ]
  puts $id "# photometry config file for xebm.pl script"
  puts $id "# miroslav.broz@usa.net, http://www.astrohk.cz, Feb 9th 1999"
  puts $id "#"
  puts $id "# parameters: name,files,date,cmp,df,ff,ap,var,konve,darkbat,"
  puts $id "#             flatbat,muniphot,munimatch,munilist,xebm"
  puts $id $name
  puts $id $files
  puts $id $date
  puts $id $cmp
  puts $id $df
  puts $id $ff
  puts $id $ap
  puts $id $var
  puts $id $konve
  puts $id $darkbat
  puts $id $flatbat
  puts $id $muniphot
  puts $id $munimatch
  puts $id $munilist
  puts $id $xebm
  close $id
  return
}

